package com.gy.mbaselibrary.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者：高业
 * 邮箱：15234150168@163.com
 * 时间：2018/2/26
 * 类描述：
 * 修改人：
 * 修改时间：2018/2/26
 * 修改备注：
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
    private static String TABLE_NAME = "xwzj.db";
    private static int TABLE_VERSION = 1;
    private static List<Class> DB_CLASSES;
    private static List<Class> UP_CLASSES;





    private Map<String, Dao> daos = new HashMap<String, Dao>();


    private DatabaseHelper(Context context) {
        super(context, TABLE_NAME, null, TABLE_VERSION);
    }


    public static void init(String tbname, int tbversion, List<Class> dbclasses,List<Class> upclasses){
        TABLE_NAME = tbname;
        TABLE_VERSION = tbversion;
        DB_CLASSES = dbclasses;
        UP_CLASSES = upclasses;
    }

    @Override
    public void onCreate(SQLiteDatabase database,
                         ConnectionSource connectionSource) {
        try {
            if (DB_CLASSES != null){
                for (int i = 0;i<DB_CLASSES.size();i++){
                    TableUtils.createTableIfNotExists(connectionSource, DB_CLASSES.get(i));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase database,
                          ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            if (newVersion > oldVersion){
                if (UP_CLASSES != null){
                    for (int i = 0;i<UP_CLASSES.size();i++){
                        TableUtils.dropTable(connectionSource, UP_CLASSES.get(i), true);
                    }
                }
            }
            onCreate(database, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private static DatabaseHelper instance;
    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized DatabaseHelper getHelper(Context context) {
        context = context.getApplicationContext();
        if (instance == null) {
            synchronized (DatabaseHelper.class) {
                if (instance == null)
                    instance = new DatabaseHelper(context);
            }
        }
        return instance;
    }
    public synchronized Dao getDao(Class clazz) throws SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();
        if (daos.containsKey(className)) {
            dao = daos.get(className);
        }
        if (dao == null) {
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }
        return dao;
    }
    /**
     * 释放资源
     */
    @Override
    public void close() {
        super.close();
        for (String key : daos.keySet()) {
            Dao dao = daos.get(key);
            dao = null;
        }
    }
}
